<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
        <!-- 状态列 -->
        <template #is_global="{ record }">
          <a-switch
            :checked-value="1"
            unchecked-value="0"
            @change="changeGlobal($event, record.id)"
            :default-checked="record.is_global == 1"
          />
        </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import addressLib from '@/api/logistics/addressLib'
import { Message } from '@arco-design/web-vue'

const crudRef = ref()
const changeGlobal = async (is_global, id) => {
    const response = await addressLib.changeGlobal({ id, is_global})
    if (response.success) {
      Message.success(response.message)
    }
  }

const options = reactive({
  id: 'address_lib',
  pageLayout: 'fixed',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: addressLib.getList,
  add: {
    show: true,
    api: addressLib.save,
    auth: ['logistics:addressLib:save']
  },
  edit: {
    show: true,
    api: addressLib.update,
    auth: ['logistics:addressLib:update']
  },
  delete: {
    show: true,
    api: addressLib.deletes,
    auth: ['logistics:addressLib:delete']
  }
})

const columns = reactive([
  {
    title: "主键",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入主键"
    }
  },
  {
    title: "名称",
    dataIndex: "name",
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入名称"
    }
  },
  {
    title: "全局显示",
    dataIndex: "is_global",
    formType: "switch",
    checkedValue: 1,
    uncheckedValue: 0
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "textarea",
    hide: true
  }
])
</script>
<script> export default { name: 'logistics:addressLib' } </script>